package cn.wolfcode.shop.cloud.redis.key;

import lombok.Getter;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * Created by wolfcode-lanxw
 */
@Getter
public enum SeckillRedisKey {
    USER_SECKILL_RECORD("USER_SECKILL_RECORD:"),
    SECKILL_STOCK_COUNT_HASH("SECKILL_STOCK_COUNT_HASH"),
    SECKILL_RANDOM_PATH("SECKILL_RANDOM_PATH:", 2, TimeUnit.SECONDS),
    SECKILL_VERIFY_CODE("SECKILL_VERIFY_CODE:", 5, TimeUnit.MINUTES),
    SECKILL_GOODS_HASH("SECKILL_GOODS_HASH");

    private String prefix;
    private long expireTime;
    private TimeUnit unit;

    SeckillRedisKey(String prefix) {
        this(prefix, 0, null);
    }

    SeckillRedisKey(String prefix, long expireTime, TimeUnit unit) {
        this.prefix = prefix;
        this.expireTime = expireTime;
        this.unit = unit;
    }

    public String join() {
        return join(null);
    }

    public String join(String...keys) {
        if (keys == null || keys.length == 0) {
            return this.prefix;
        }

        return this.prefix + StringUtils.join(keys, ":");
    }
}
